{extend name="common/layout"}

{block name="main"}
<div class="layui-card">
    <div class="layui-card-body">
        <table id="power-table" lay-filter="power-table"></table>
    </div>
</div>

<script type="text/html" id="power-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
    <button class="pear-btn pear-btn-success pear-btn-md" lay-event="expandAll">
        <i class="layui-icon layui-icon-spread-left"></i>
        展开
    </button>
    <button class="pear-btn pear-btn-success pear-btn-md" lay-event="foldAll">
        <i class="layui-icon layui-icon-shrink-right"></i>
        折叠
    </button>
</script>

<script type="text/html" id="power-bar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i></button>
</script>

<script type="text/html" id="power-type">
    {{#if (d.type == '0') { }}
    <span>目录</span>
    {{# }else if(d.type == '1'){ }}
    <span>菜单</span>
    {{# } }}
</script>

<script type="text/html" id="power-enable">
    {{#if (d.is_show == '0') { }}
    <span class="layui-badge">隐藏</span>
    {{# }else if(d.is_show == '1'){ }}
    <span class="layui-badge layui-bg-green">显示</span>
    {{# } }}

</script>

<script type="text/html" id="icon">
    <i class="layui-icon {{d.icon}}"></i>
</script>
{/block}

{block name="footer_script"}
<script>
    layui.use(['table','form','jquery','treetable'],function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let treetable = layui.treetable;

        let MODULE_PATH = "/admin/menu";

        window.render = function(){
            treetable.render({
                treeColIndex: 0,
                treeSpid: 0,
                treeIdName: 'id',
                treePidName: 'pid',
                skin:'line',
                treeDefaultClose: false,
                toolbar:'#power-toolbar',
                elem: '#power-table',
                url: MODULE_PATH + '/list',
                page: false,
                cols: [
                    [
                        {field: 'id',title: 'ID'},
                        {field: 'title', minWidth: 200, title: '菜单名称'},
                        {field: 'icon', title: '图标',templet:'#icon'},
                        {field: 'app', title: 'app'},
                        {field: 'controller', title: 'controller'},
                        {field: 'action', title: 'action'},
                        {field: 'type', title: '权限类型',templet:'#power-type'},
                        {field: 'is_show', title: '是否显示',templet:'#power-enable'},
                        {field: 'sort', title: '排序'},
                        {title: '操作',templet: '#power-bar', width: 150, align: 'center'}
                    ]
                ]
            });
        }

        render();

        table.on('tool(power-table)',function(obj){
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }
        })


        table.on('toolbar(power-table)', function(obj){
            if(obj.event === 'add'){
                window.add();
            } else if(obj.event === 'refresh'){
                window.refresh();
            } else if(obj.event === 'expandAll'){
                treetable.expandAll("#power-table");
            } else if(obj.event === 'foldAll'){
                treetable.foldAll("#power-table");
            }
        });

        window.add = function(){
            layer.open({
                type: 2,
                title: '新增',
                shade: 0.1,
                area: ['650px', '600px'],
                content: MODULE_PATH + '/edit?id=0',
                end: function(index, layero){ //弹窗销毁事件
                    //parent.layui.admin.refreshThis()
                }
            });
        }

        window.edit = function(obj){
            layer.open({
                type: 2,
                title: '修改',
                shade: 0.1,
                area: ['650px', '600px'],
                content: MODULE_PATH + '/edit?id='+obj.data.id,
                end: function(index, layero){
                    //parent.layui.admin.refreshThis()
                }
            });
        }
        window.remove = function(obj){
            layer.confirm('确定要删除该菜单', {icon: 3, title:'提示'}, function(index){
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH+"/del?id="+obj.data['id'],
                    dataType:'json',
                    type:'delete',
                    success:function(res){
                        layer.close(loading);
                        if(res.code == 1){
                            layer.msg(res.msg,{icon:1,time:1000},function(){
                                obj.del();
                            });
                        }else{
                            layer.msg(res.msg,{icon:2,time:1000});
                        }
                    }
                })
            });
        }


    })
</script>
{/block}